package com.xinghai.web.controller.dingtalk;

import com.xinghai.common.annotation.Anonymous;
import com.xinghai.common.annotation.Log;
import com.xinghai.common.core.controller.BaseController;
import com.xinghai.common.core.domain.AjaxResult;
import com.xinghai.common.core.page.TableDataInfo;
import com.xinghai.common.enums.BusinessType;
import com.xinghai.common.exception.ServiceException;
import com.xinghai.common.utils.StringUtils;
import com.xinghai.common.utils.poi.ExcelUtil;
import com.xinghai.system.domain.XhVehicleInfoMaterial;
import com.xinghai.system.domain.dto.XhVehicleInfoMaterialNeedDTO;
import com.xinghai.system.domain.dto.XhVehicleInfoMaterialPuckDTO;
import com.xinghai.system.domain.vo.XhVehicleInfoMaterialVO;
import com.xinghai.system.service.IXhVehicleInfoMaterialService;
import com.xinghai.system.util.XHConstant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 用车耗材明细Controller
 * 
 * @author smy
 * @date 2024-07-25
 */
@RestController
@RequestMapping("/vehicle/material")
public class XhVehicleInfoMaterialController extends BaseController
{
    @Autowired
    private IXhVehicleInfoMaterialService xhVehicleInfoMaterialService;

    /**
     * 查询用车耗材明细列表
     */
    @GetMapping("/list")
    public TableDataInfo list(XhVehicleInfoMaterial xhVehicleInfoMaterial)
    {
        startPage();
        List<XhVehicleInfoMaterial> list = xhVehicleInfoMaterialService.list(xhVehicleInfoMaterial);
        return getDataTable(list);
    }

    /**
     * 导出用车耗材明细列表
     */
    @Log(title = "用车耗材明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, XhVehicleInfoMaterial xhVehicleInfoMaterial)
    {
        List<XhVehicleInfoMaterial> list = xhVehicleInfoMaterialService.list(xhVehicleInfoMaterial);
        ExcelUtil<XhVehicleInfoMaterial> util = new ExcelUtil<XhVehicleInfoMaterial>(XhVehicleInfoMaterial.class);
        util.exportExcel(response, list, "用车耗材明细数据");
    }

    /**
     * 获取用车耗材明细详细信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo(Long id)
    {
        return success(xhVehicleInfoMaterialService.getInfo(id));
    }

    /**
     * 新增用车耗材明细
     */
    @Log(title = "用车耗材明细", businessType = BusinessType.INSERT)
    @PostMapping("/insert")
    public AjaxResult add(@RequestBody XhVehicleInfoMaterial xhVehicleInfoMaterial)
    {
        return toAjax(xhVehicleInfoMaterialService.insert(xhVehicleInfoMaterial));
    }

    /**
     * 新增用车耗材明细
     */
//    @PreAuthorize("@ss.hasPermi('system:material:add')")
//    @Log(title = "用车耗材明细", businessType = BusinessType.INSERT)
    @PostMapping("/addList")
    @Anonymous
    public AjaxResult addList(@RequestBody XhVehicleInfoMaterialVO xhVehicleInfoMaterial) throws UnknownHostException {
        return toAjax(xhVehicleInfoMaterialService.saveList(xhVehicleInfoMaterial));
    }

    /**
     * 修改用车耗材明细
     */
    @Log(title = "用车耗材明细", businessType = BusinessType.UPDATE)
    @PostMapping("/update")
    public AjaxResult update(@RequestBody XhVehicleInfoMaterial xhVehicleInfoMaterial)
    {
        return toAjax(xhVehicleInfoMaterialService.update(xhVehicleInfoMaterial));
    }

    /**
     * 删除用车耗材明细
     */
    @Log(title = "用车耗材明细", businessType = BusinessType.DELETE)
	@PostMapping("/delete")
    public AjaxResult delete(Long id)
    {
        return toAjax(xhVehicleInfoMaterialService.delete(id));
    }


    /**
     * 批量删除用车耗材明细
     */
    @Log(title = "用车耗材明细", businessType = BusinessType.DELETE)
    @PostMapping("/deleteBatch")
    public AjaxResult deleteBatch(Long[] ids)
    {
        return toAjax(xhVehicleInfoMaterialService.deleteBatch(ids));
    }

    /**
     * 导入用车耗材明细模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<XhVehicleInfoMaterial> util = new ExcelUtil<XhVehicleInfoMaterial>(XhVehicleInfoMaterial.class);
        util.importTemplateExcel(response, "用车耗材明细数据模板");
    }


    /**
     * 导入用车耗材明细
     */
    @Log(title = "用车耗材明细管理", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file)
    {
        ExcelUtil<XhVehicleInfoMaterial> util = new ExcelUtil<XhVehicleInfoMaterial>(XhVehicleInfoMaterial.class);
        List<XhVehicleInfoMaterial> xhVehicleInfoMaterialList = null;
        try {
            xhVehicleInfoMaterialList = util.importExcel(file.getInputStream());
        } catch (IOException e) {
            throw new ServiceException("导入数据失败");
        }
        return success(xhVehicleInfoMaterialService.importData(xhVehicleInfoMaterialList));
    }

    /**
     * 查询用车耗材明细列表
     */
    @GetMapping("/list/by/type/and/vehicleId")
    public AjaxResult getListByTypeAndVehicleId(XhVehicleInfoMaterial xhVehicleInfoMaterial)
    {
        List<XhVehicleInfoMaterial> list = xhVehicleInfoMaterialService.lambdaQuery()
                .eq(StringUtils.isNotNull(xhVehicleInfoMaterial.getVehicleId()),XhVehicleInfoMaterial::getVehicleId,xhVehicleInfoMaterial.getVehicleId())
                .eq(StringUtils.isNotNull(xhVehicleInfoMaterial.getType()),XhVehicleInfoMaterial::getType,xhVehicleInfoMaterial.getType())
                .eq(XhVehicleInfoMaterial::getDelFlag, XHConstant.DEL_FLAG_NORMAL)
                .orderByAsc(XhVehicleInfoMaterial::getCreateTime)
                .list();
        return success(list);
    }

    /**
     * 批量保存耗材明细列表
     */
    @PostMapping("/save/batch/material")
    public AjaxResult saveBatchMaterial(@RequestBody XhVehicleInfoMaterialVO xhVehicleInfoMaterialVO)
    {
        return success(xhVehicleInfoMaterialService.saveBatchMateial(xhVehicleInfoMaterialVO));
    }

    /**
     * 查询用车耗材明细列表根据vehicleId
     */
    @GetMapping("/getMaterialList/{vehicleId}")
    public TableDataInfo getMaterialList(@PathVariable("vehicleId") Long vehicleId)
    {
        startPage();
        List<XhVehicleInfoMaterial> list = xhVehicleInfoMaterialService.getMaterialList(vehicleId);
        return getDataTable(list);
    }

}
